package com.tywj.buscustomerapp.view.activity;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.MonthTBean;
import com.tywj.buscustomerapp.model.bean.MyTicketBean;
import com.tywj.buscustomerapp.model.bean.TravelBean;
import com.tywj.buscustomerapp.utils.SPUtils;
import com.tywj.buscustomerapp.utils.SortComparatorDown;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.utils.myadapter.CommonRcvAdapter;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.adapter.ViewPagerAdapter;
import com.tywj.buscustomerapp.view.adapter.ViewPagerTicketAdapter;
import com.tywj.buscustomerapp.view.fragment.MyTravelMonthTicketFragment;
import com.tywj.buscustomerapp.view.fragment.MyTravelTicketFragment;
import com.tywj.buscustomerapp.view.fragment.SchoolFragment;
import com.tywj.buscustomerapp.view.item.MyTravelItem;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.tywj.buscustomerapp.view.myview.TaylorViewPager;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyTravelActivity extends BaseActivity {
    @BindView(R.id.head_left) ImageView back;
    @BindView(R.id.head_title)
    TextView title;
    RecyclerView recyclerView;
    SmartRefreshLayout srl;
    RelativeLayout errorLayout;
    private CommonRcvAdapter<TravelBean.DataBean.ResultListBean> adapter;
    private List<TravelBean.DataBean.ResultListBean> beans  = new ArrayList<>();
    ImageView right_img;
    private Animation translateAnimation;
    private int page = 1;


    @Override
    protected int getContentId() {
        return R.layout.activity_my_travel;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText("我的行程");
        recyclerView =findViewById(R.id.recycler);
        srl = findViewById(R.id.refresh);
        errorLayout = findViewById(R.id.error);
        adapter  = new CommonRcvAdapter<TravelBean.DataBean.ResultListBean>(beans) {
            @NonNull
            @Override
            public AdapterItem createItem(Object type) {
                return new MyTravelItem();
            }
        };
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        super.initData();
        showLoading();
        doGetInfo();
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                doGetInfo();
            }
        });
        srl.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                doGetInfo();
            }
        });
    }

    private  void doGetInfo(){
        RequestQueue requestQueue = Volley.newRequestQueue(MyTravelActivity.this);
        //创建一个请求
        String url = Config.SBURL + "/order/getMyTrip";
        JSONObject object = new JSONObject();
        try {
            object.put("yhm", SPUtils.getUserPhone(getApplicationContext()));
            object.put("page", page);
            object.put("size", "30");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                closeLoading();
//                Log.i("taylor", String.valueOf(jsonObject));
                TravelBean data = new Gson().fromJson(String.valueOf(jsonObject), TravelBean.class);
                if (srl.isRefreshing())
                    srl.finishRefresh();
                if (srl.isLoading())
                    srl.finishLoadmore();
                if (data!=null && data.getData().getResultList()!=null){
                    if (page==1) {
                        if (data.getData().getResultList().size()>0) {
                            beans.clear();
                            beans = data.getData().getResultList();
                        }else
                            errorLayout.setVisibility(View.VISIBLE);
                    }else{
                        beans.addAll(data.getData().getResultList());
                    }
                    adapter.setData(beans);
                    adapter.notifyDataSetChanged();
                }else{
                    errorLayout.setVisibility(View.VISIBLE);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                closeLoading();
                if (srl.isRefreshing())
                    srl.finishRefresh();
                if (srl.isLoading())
                    srl.finishLoadmore();
                errorLayout.setVisibility(View.VISIBLE);
            }
        });
        //将创建的请求添加到队列中
        requestQueue.add(jsonObjectRequest);
    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private LoadingDialog waitDialog;
    public void showLoading() {
        if (waitDialog==null)
            waitDialog = new LoadingDialog(MyTravelActivity.this);
        if (!waitDialog.isShowing())
            waitDialog.show();
    }

    public void closeLoading() {
        if (waitDialog!=null && waitDialog.isShowing())
            waitDialog.dismiss();
    }



    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("我的行程");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("我的行程");
        MobclickAgent.onPause(this);
    }

}
